package com.ruoyi.manage.service.impl;

import java.util.List;

import com.ruoyi.manage.domain.SurgeryForm;
import com.ruoyi.manage.mapper.FormSettingBigMapper;
import com.ruoyi.manage.service.IFormSettingBigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.manage.domain.FormSettingBig;
import com.ruoyi.manage.mapper.FormSettingMapper;
import com.ruoyi.manage.domain.FormSetting;
import com.ruoyi.manage.service.IFormSettingService;

/**
 * 表单配置Service业务层处理
 *
 * @author zww
 * @date 2025-07-14
 */
@Service
public class FormSettingServiceImpl implements IFormSettingService {
    @Autowired
    private FormSettingMapper formSettingMapper;
    @Autowired
    private IFormSettingBigService formSettingBigService;
    @Autowired
    private FormSettingBigMapper formSettingBigMapper;
    /**
     * 查询表单配置
     *
     * @param formSettingId 表单配置主键
     * @return 表单配置
     */
    @Override
    public FormSetting selectFormSettingByFormSettingId(Long formSettingId) {
        FormSetting formSetting = formSettingMapper.selectFormSettingByFormSettingId(formSettingId);
        FormSettingBig formSettingBig = new FormSettingBig();
        formSettingBig.setFormSettingId(formSettingId);
        formSetting.setFormSettingBigList(formSettingBigService.selectFormSettingBigList(formSettingBig));
        return formSetting;
    }

    /**
     * 查询表单配置列表
     *
     * @param formSetting 表单配置
     * @return 表单配置
     */
    @Override
    public List<FormSetting> selectFormSettingList(FormSetting formSetting) {
        return formSettingMapper.selectFormSettingList(formSetting);
    }

    /**
     * 新增表单配置
     *
     * @param formSetting 表单配置
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFormSetting(FormSetting formSetting) {
        int rows = formSettingMapper.insertFormSetting(formSetting);
        insertFormSettingBig(formSetting);
        return rows;
    }

    /**
     * 修改表单配置
     *
     * @param formSetting 表单配置
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFormSetting(FormSetting formSetting) {
        updateFormSettingBig(formSetting);
        return formSettingMapper.updateFormSetting(formSetting);
    }

    public void updateFormSettingBig(FormSetting formSetting) {
        List<FormSettingBig> formSettingBigList = formSetting.getFormSettingBigList();
        if (formSettingBigList==null|| formSettingBigList.isEmpty()) return;
        for (FormSettingBig formSettingBig : formSettingBigList) {
            if (formSettingBig.getFormSettingId() != null)
                formSettingBigMapper.updateFormSettingBig(formSettingBig);

            else{
                formSettingBig.setFormSettingId(formSetting.getFormSettingId());
                formSettingBigMapper.insertFormSettingBig(formSettingBig);
            }
        }
    }

    /**
     * 批量删除表单配置
     *
     * @param formSettingIds 需要删除的表单配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFormSettingByFormSettingIds(Long[] formSettingIds) {
        formSettingMapper.deleteFormSettingBigByFormSettingIds(formSettingIds);
        return formSettingMapper.deleteFormSettingByFormSettingIds(formSettingIds);
    }

    /**
     * 删除表单配置信息
     *
     * @param formSettingId 表单配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFormSettingByFormSettingId(Long formSettingId) {
        formSettingMapper.deleteFormSettingBigByFormSettingId(formSettingId);
        return formSettingMapper.deleteFormSettingByFormSettingId(formSettingId);
    }

    @Override
    public FormSetting selectForm(SurgeryForm surgeryForm) {
        FormSetting formSetting = formSettingMapper.selectFormSettingByFormSettingId(surgeryForm.getFormSettingId());
        formSetting.setFormSettingBigList(formSettingBigService.selectFormSettingBigWithValue(surgeryForm.getFormSettingId(), surgeryForm.getSurgeryFormId()));
        return formSetting;
    }

    /**
     * 新增单大项信息
     *
     * @param formSetting 表单配置对象
     */
    public void insertFormSettingBig(FormSetting formSetting) {
        List<FormSettingBig> formSettingBigList = formSetting.getFormSettingBigList();
        Long formSettingId = formSetting.getFormSettingId();
        if (StringUtils.isNotNull(formSettingBigList)) {
            List<FormSettingBig> list = new ArrayList<>();
            for (FormSettingBig formSettingBig : formSettingBigList) {
                formSettingBig.setFormSettingId(formSettingId);
                list.add(formSettingBig);
            }
            if (list.size() > 0) {
                formSettingMapper.batchFormSettingBig(list);
            }
        }
    }
}
